﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace library
{
    public partial class up_stage : Form
    {
        public MySqlConnection conn = new DBclass().getConn();
        public up_stage()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            conn.Open();
            string bookname = textBox4.Text;
            string sql = "SELECT book_info.bID,book_info.ISBN,book1_info.bname,book_info.on_whether FROM book1_info, book_info where book_info.on_whether='否' and book_info.ISBN=book1_info.ISBN AND book1_info.bname like @name GROUP BY book_info.bID";
            MySqlCommand cmd = new MySqlCommand(sql, conn);
            cmd.Parameters.Add("@name", MySqlDbType.VarChar, 100).Value = "%" + bookname + "%";
            MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
            DataSet dataSet = new DataSet();
            adapter.Fill(dataSet);
            DataTable dataTable = dataSet.Tables[0];
            dataGridView5.AutoGenerateColumns = false;
            dataGridView5.DataSource = dataTable;
            this.dataGridView5.Columns["图书编号"].DataPropertyName = dataTable.Columns["bID"].ToString();
            this.dataGridView5.Columns["ISBN"].DataPropertyName = dataTable.Columns["ISBN"].ToString();
            this.dataGridView5.Columns["图书名称"].DataPropertyName = dataTable.Columns["bname"].ToString();
            this.dataGridView5.Columns["是否上架"].DataPropertyName = dataTable.Columns["on_whether"].ToString();
            conn.Close();
        }

        private void button16_Click(object sender, EventArgs e)
        {
            if (dataGridView5.Rows.Count > 0)//反选
            {
                dataGridView5.EndEdit();//结束编辑状态
                foreach (DataGridViewRow row in dataGridView5.Rows)
                {
                    row.Cells[0].Value = !(true.Equals(row.Cells[0].Value));

                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            conn.Open();
            if (dataGridView5.Rows.Count > 0)
            {
                int j = 0;
                for (int i = 0; i < dataGridView5.Rows.Count; i++)
                {
                    DataGridViewCheckBoxCell dataGridViewCheckBoxCell = (DataGridViewCheckBoxCell)dataGridView5.Rows[i].Cells[0];
                    Boolean flag = Convert.ToBoolean(dataGridViewCheckBoxCell.Value);
                    if (flag)
                    {
                        string bID = dataGridView5.Rows[i].Cells[1].Value.ToString();
                        string sql1 = "update book_info set on_whether='是' where bID=@bID";
                        MySqlCommand cmd1 = new MySqlCommand(sql1, conn);
                        cmd1.Parameters.Add("@bID", MySqlDbType.Int32).Value = bID;
                        j += cmd1.ExecuteNonQuery();
                        dataGridView5.Rows[i].DefaultCellStyle.BackColor = Color.Red;
                    }
                }
                if (j > 0)
                {
                    MessageBox.Show("上架成功！");
                }
                conn.Close();
            }
        }

        private void button17_Click(object sender, EventArgs e)
        {
            down_stage f=new down_stage();
            f.Show();
            this.Close();
        }
    }
}
